package com.jstyle.jclifexd.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.activity.BaseActivity;


import com.jstyle.jclifexd.adapter.AboutAdapter;
import com.jstyle.jclifexd.adapter.RecyclerViewBaseAdapter;
import com.jstyle.jclifexd.utils.DateUtil;
import com.jstyle.jclifexd.utils.SDUtil;
import com.jstyle.jclifexd.utils.SchedulersTransformer;
import com.jstyle.jclifexd.utils.ScreenUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 关于
 */
public class AboutUsActivity extends BaseActivity implements RecyclerViewBaseAdapter.OnItemClickListener {

    @BindView(R.id.RecyclerView_about)
    RecyclerView RecyclerViewAbout;
    @BindArray(R.array.about_array)
    String[] aboutArray;
    @BindView(R.id.bt_goal_back)
    Button btGoalBack;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_share)
    ImageView ivShare;

    private String PrivcyUrl = "https://getfomofit.com/privacy";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        btGoalBack.setText(R.string.about_us);
        ScreenUtils.setTitleTypeface(btGoalBack);
        ivShare.setVisibility(View.GONE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        AboutAdapter aboutAdapter = new AboutAdapter(Arrays.asList(aboutArray));
        aboutAdapter.setOnItemClickListener(this);
        RecyclerViewAbout.setAdapter(aboutAdapter);
        RecyclerViewAbout.addItemDecoration(dividerItemDecoration);
        RecyclerViewAbout.setLayoutManager(linearLayoutManager);

    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onItemClick(int position) {
        switch (position) {
//            case 0:
//                startActivity(HelpActivity.class);
//                break;
            case 0:
             //  mergeFile();
                break;
            case 1:
              //  startActivity(ProvicyAboutActivity.class);
                toProvicy();
                break;
        }
    }
    private void mergeFile() {
        File file = new File(SDUtil.btCommandLog);
        if (!file.exists()) return;
        String[] fileList = file.list();
        long time = System.currentTimeMillis();
        final List<File> list = new ArrayList<>();
        for (String fileName : fileList) {
            String date = fileName.replace(".txt", "");
            if ("log".equals(date)) continue;
            long fileMills = DateUtil.getDateLong(date);
            if (time - fileMills <= 7 * DateUtil.oneDayMillis) {
                list.add(new File(file, fileName));
            }
        }
        final File fileTarget = new File(SDUtil.btCommandLog + File.separator + "log.txt");

        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (fileTarget.exists()) fileTarget.delete();
                fileTarget.createNewFile();
                mergeFiles2(fileTarget,list);
                emitter.onComplete();
            }
        }).compose(SchedulersTransformer.io2MainObservable()).subscribe(new Observer<Object>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object o) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                shareByPhone(fileTarget);
            }
        });
    }

    public void mergeFiles2(File realFile, List<File> tempFiles) {
        FileChannel mFileChannel;
        try {
            FileOutputStream fos = new FileOutputStream(realFile);
            mFileChannel = fos.getChannel();
            FileChannel inFileChannel;
            for (File file : tempFiles) {
                inFileChannel = new FileInputStream(file).getChannel();
                inFileChannel.transferTo(0, inFileChannel.size(), mFileChannel);
                inFileChannel.close();
            }
            fos.close();
            mFileChannel.close();
        } catch (Exception e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void shareByPhone(File file) {
        Uri imageUri = null;
        if (Build.VERSION.SDK_INT >= 24) {
            imageUri = FileProvider.getUriForFile(getApplicationContext(),
                    getString(R.string.fileprovider), file);
        } else {
            imageUri = Uri.fromFile(file);
        }
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType("*/*");
        startActivity(Intent.createChooser(shareIntent, "分享到"));
    }


    private void toProvicy() {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(PrivcyUrl);
        intent.setData(content_url);
        startActivity(intent);

    }
}
